/*
电量统计,创建一个秒定时器进行电量统计累加，确保精确的时间
Library: librt
*/
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "types.h"
#include "dataBase/elecQuttFunction.h"
#include "electricity_quantity_statistics.h"
#include "function_interface.h"
#include "table_refresh.h"
#include "debug.h"
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>
#include "misc.h"


#define EPSION 0.000001F
#define ELEC_MICRO_FILE_ID 2 

typedef enum{
    FLAG_NO_CHANGE,
    FLAG_CHANGE = !FLAG_NO_CHANGE,
} flagChange_t;

struct elecQuttDetail_s{
    float total;
    float yesterday;
    float hour;
    float day;
    float month;
    float year;
};

typedef struct elecQuttData_s{
    struct elecQuttDetail_s ac;
    struct elecQuttDetail_s bat;
} elecQuttData_t;

typedef struct elecQuttPerSec_s{
    float ac;
    float bat;
} elecQuttPerSec_t;

static elecQuttData_t chrgElecQuttData, discElecQuttData;
static elecQuttPerSec_t elecQuttPerSec;
static float eqsDiscAcData, eqsDiscBatData, eqsChrgAcData, eqsChrgBatData;
static struct elecQuttDataDB eqsQuttDataDB;

static struct tm eqsTm;
static uint32_t testCnt;

// static flagChange_t isSecChanged(struct tm* curTm){
    
//     if(curTm->tm_sec != eqsTm.tm_sec){
//         eqsTm.tm_min = curTm->tm_min;
//         eqsTm.tm_sec = curTm->tm_sec;
//         return FLAG_CHANGE;
//     }
//     else{
//         return FLAG_NO_CHANGE;
//     }
// }

static flagChange_t isHourChanged(struct tm* curTm){
    
    if(curTm->tm_hour != eqsTm.tm_hour){
        eqsTm.tm_hour = curTm->tm_hour;
        return FLAG_CHANGE;
    }
    else{
        return FLAG_NO_CHANGE;
    }
}

static flagChange_t isDayChanged(struct tm* curTm){

    if(curTm->tm_mday != eqsTm.tm_mday){
        eqsTm.tm_mday = curTm->tm_mday;
        return FLAG_CHANGE;
    }
    else{
        return FLAG_NO_CHANGE;
    }
}

static flagChange_t isMonthChanged(struct tm* curTm){
    
    if(curTm->tm_mon != eqsTm.tm_mon){
        eqsTm.tm_mon = curTm->tm_mon;
        return FLAG_CHANGE;
    }
    else{
        return FLAG_NO_CHANGE;
    }
}

static flagChange_t isYearChanged(struct tm* curTm){
    
    if(curTm->tm_year != eqsTm.tm_year){
        eqsTm.tm_year = curTm->tm_year;
        return FLAG_CHANGE;
    }
    else{
        return FLAG_NO_CHANGE;
    }
}

//每秒统计电量线程
void statisticsTimerThread(int signum){

    float acPwr, batPwr;
    fpagDataFsm_t fsm;
#if 1
    FuncInf_GetFsm(FUNCINF_FSM_TYPE_THIS_TIME, &fsm);

    if(fsm > FUNCINF_FSM_IDLE && fsm < FUNCINF_FSM_FAULT){
        FuncInf_GetPower(FUNCINF_PWR_TYPE_ACT, &acPwr);
        FuncInf_GetPower(FUNCINF_PWR_TYPE_BAT, &batPwr);

        elecQuttPerSec.ac = acPwr / SEC_PER_HOUR;
        elecQuttPerSec.bat = batPwr / SEC_PER_HOUR;
    }
    else{
        elecQuttPerSec.ac = 0;
        elecQuttPerSec.bat = 0;
    }
#endif

    if(elecQuttPerSec.ac > EPSION){
        discElecQuttData.ac.total += elecQuttPerSec.ac;
        discElecQuttData.ac.hour += elecQuttPerSec.ac;
        discElecQuttData.ac.day += elecQuttPerSec.ac;
        discElecQuttData.ac.month += elecQuttPerSec.ac;
        discElecQuttData.ac.year += elecQuttPerSec.ac;
    }
    else if(elecQuttPerSec.ac < -EPSION){
        elecQuttPerSec.ac *= -1;
        chrgElecQuttData.ac.total += elecQuttPerSec.ac;
        chrgElecQuttData.ac.hour += elecQuttPerSec.ac;
        chrgElecQuttData.ac.day += elecQuttPerSec.ac;
        chrgElecQuttData.ac.month += elecQuttPerSec.ac;
        chrgElecQuttData.ac.year += elecQuttPerSec.ac;
    }

    if(elecQuttPerSec.bat > EPSION){
        discElecQuttData.bat.total += elecQuttPerSec.bat;
        discElecQuttData.bat.hour += elecQuttPerSec.bat;
        discElecQuttData.bat.day += elecQuttPerSec.bat;
        discElecQuttData.bat.month += elecQuttPerSec.bat;
        discElecQuttData.bat.year += elecQuttPerSec.bat;
    }
    else if(elecQuttPerSec.bat < -EPSION){
        elecQuttPerSec.bat *= -1;
        chrgElecQuttData.bat.total += elecQuttPerSec.bat;
        chrgElecQuttData.bat.hour += elecQuttPerSec.bat;
        chrgElecQuttData.bat.day += elecQuttPerSec.bat;
        chrgElecQuttData.bat.month += elecQuttPerSec.bat;
        chrgElecQuttData.bat.year += elecQuttPerSec.bat;
    }
    testCnt++;
    Misc_RecordElecIndex(testCnt, fsm, (int)acPwr);
    TableRefresh_SendMessage(TABLE_MSGVAL_ELEC_QUTT);
}

//初始化定时器
int timerInit(timer_t* timerId, struct sigevent* sevp, struct itimerspec* it){

    if(!timerId || !sevp || !it) 
        return -1;

    memset(sevp, 0, sizeof(struct sigevent));
    sevp->sigev_signo = SIGALRM;
    sevp->sigev_notify = SIGEV_SIGNAL;
    sevp->sigev_value.sival_ptr = timerId;    

    if(timer_create(CLOCK_MONOTONIC, sevp, timerId) == -1){
        perror("timer_create failed!\n");
        return -1;
    }

    return 0;
}

//定时器，启动
int timerStart(timer_t* timerId, struct itimerspec* it){

    if(!timerId || !it)
        return -1;

    it->it_interval.tv_sec = 1;    //1秒钟的时间间隔
    it->it_interval.tv_nsec = 0;
    it->it_value.tv_sec = 2;    //暂定为5秒钟后启动定时器
    it->it_value.tv_nsec = 0;
    
    if(timer_settime(*timerId, 0, it, NULL) == -1){
        perror("timer_settime failed!\n");
        return -1;
    }

    return 0;
}

//清理电量数据
void eqsQuttDataClear(){

    memset(&discElecQuttData, 0, sizeof(discElecQuttData));
    memset(&chrgElecQuttData, 0, sizeof(chrgElecQuttData));
    deleteElecQuttData_db();

}


//eqsDiscAcData, eqsDiscBatData, eqsChrgAcData, eqsChrgBatData;
static flagChange_t isDiscAcElecDataChanged(){
    
    printf("discElecQuttData.ac.total is %.6f, eqsDiscAcData is %.6f\n", discElecQuttData.ac.total, eqsDiscAcData);
    if(discElecQuttData.ac.total - eqsDiscAcData > EPSION){
        eqsDiscAcData = discElecQuttData.ac.total;
        printf("disc ac total is changed\n");
        return FLAG_CHANGE;
    }
    else{
        return FLAG_NO_CHANGE;
    }
}

static flagChange_t isDiscBatElecDataChanged(){
    
    printf("discElecQuttData.bat.total is %.6f, eqsDiscBatData is %.6f\n", discElecQuttData.bat.total, eqsDiscBatData);
    if(discElecQuttData.bat.total - eqsDiscBatData > EPSION){
        eqsDiscBatData = discElecQuttData.bat.total;
        printf("disc bat total is changed\n");
        return FLAG_CHANGE;
    }
    else{
        return FLAG_NO_CHANGE;
    }
}

static flagChange_t isChrgAcElecDataChanged(){
    
    printf("chrgElecQuttData.ac.total is %.6f, eqsChrgAcData is %.6f\n", chrgElecQuttData.ac.total, eqsChrgAcData);
    if(chrgElecQuttData.ac.total - eqsChrgAcData > EPSION){
        eqsChrgAcData = chrgElecQuttData.ac.total;
        printf("chrg ac total is changed\n");
        return FLAG_CHANGE;
    }
    else{
        return FLAG_NO_CHANGE;
    }
}

static flagChange_t isChrgBatElecDataChanged(){
    
    printf("chrgElecQuttData.bat.total is %.6f, eqsChrgBatData is %.6f\n", chrgElecQuttData.bat.total, eqsChrgBatData);
    if(chrgElecQuttData.bat.total - eqsChrgBatData > EPSION){
        eqsChrgBatData = chrgElecQuttData.bat.total;
        printf("chrg bat total is changed\n");
        return FLAG_CHANGE;
    }
    else{
        return FLAG_NO_CHANGE;
    }
}

/*
重写电量存储逻辑
表名: ElecQuttTable 
column: year month day hour isDiscOrChrg isAcOrBat totalElecData yearElecData monthElecData dayElecData hourElecData
存储年月日时, isDiscOrChrg: DISC or CHRG, isAcOrBat: AC or BAT
totalElecData yearElecData monthElecData dayElecData: 存入数据时刻的电量
每次存储时间：59分59秒 存储之前判断数据有没有变动
*/
static void elecQuttDataSave(){

    time_t saveTime;
    struct tm saveTm;

    saveTime = time(NULL) - 3600;
    localtime_r(&saveTime, &saveTm);

    if(isDiscAcElecDataChanged() == FLAG_CHANGE){
        insertElecQuttData_db(saveTm.tm_year + 1900, saveTm.tm_mon + 1, saveTm.tm_mday, saveTm.tm_hour, ELEC_QUTT_DISC, ELEC_QUTT_AC, 
                            discElecQuttData.ac.total, discElecQuttData.ac.year, discElecQuttData.ac.month, discElecQuttData.ac.day, discElecQuttData.ac.hour);
    }
    if(isDiscBatElecDataChanged() == FLAG_CHANGE){
        insertElecQuttData_db(saveTm.tm_year + 1900, saveTm.tm_mon + 1, saveTm.tm_mday, saveTm.tm_hour, ELEC_QUTT_DISC, ELEC_QUTT_BAT, 
                            discElecQuttData.bat.total, discElecQuttData.bat.year, discElecQuttData.bat.month, discElecQuttData.bat.day, discElecQuttData.bat.hour);
    }
    if(isChrgAcElecDataChanged() == FLAG_CHANGE){
        insertElecQuttData_db(saveTm.tm_year + 1900, saveTm.tm_mon + 1, saveTm.tm_mday, saveTm.tm_hour, ELEC_QUTT_CHRG, ELEC_QUTT_AC, 
                            chrgElecQuttData.ac.total, chrgElecQuttData.ac.year, chrgElecQuttData.ac.month, chrgElecQuttData.ac.day, chrgElecQuttData.ac.hour);
    }
    if(isChrgBatElecDataChanged() == FLAG_CHANGE){
        insertElecQuttData_db(saveTm.tm_year + 1900, saveTm.tm_mon + 1, saveTm.tm_mday, saveTm.tm_hour, ELEC_QUTT_CHRG, ELEC_QUTT_BAT, 
                            chrgElecQuttData.bat.total, chrgElecQuttData.bat.year, chrgElecQuttData.bat.month, chrgElecQuttData.bat.day, chrgElecQuttData.bat.hour);
    }

}
/*
恢复数据库中的数据到内存中
*/
static void elecQuttDataRestore(struct tm* curTm){

    struct elecQuttDataDB eqData;
    time_t yesterdayTime;
    struct tm yesterdayTm;
    yesterdayTime = time(NULL) - SEC_PER_HOUR * HOUR_PER_DAY;
    localtime_r(&yesterdayTime, &yesterdayTm);

    memset(&eqData, 0, sizeof(struct elecQuttDataDB));
    strcpy(eqData.chargeType, ELEC_QUTT_DISC);
    strcpy(eqData.branchType, ELEC_QUTT_AC);
    readElecQuttData_db(&eqData, ELEC_QUTT_DATA_RESTORE);
    discElecQuttData.ac.total = eqData.totalElecData;
    eqsDiscAcData = eqData.totalElecData;
    if((curTm->tm_year + 1900) == eqData.year){
        discElecQuttData.ac.year = eqData.yearElecData;
    }
    if((curTm->tm_mon + 1) == eqData.month){
        discElecQuttData.ac.month = eqData.monthElecData;
    }
    if(curTm->tm_mday == eqData.day){
        discElecQuttData.ac.day = eqData.dayElecData;
    }
    
    eqData.dayElecData = 0;
    eqData.year = yesterdayTm.tm_year + 1900;
    eqData.month = yesterdayTm.tm_mon + 1;
    eqData.day = yesterdayTm.tm_mday;
    readElecQuttData_db(&eqData, ELEC_QUTT_DATA_DAY);
    discElecQuttData.ac.yesterday = eqData.dayElecData;

    memset(&eqData, 0, sizeof(struct elecQuttDataDB));
    strcpy(eqData.chargeType, ELEC_QUTT_DISC);
    strcpy(eqData.branchType, ELEC_QUTT_BAT);
    readElecQuttData_db(&eqData, ELEC_QUTT_DATA_RESTORE);
    discElecQuttData.bat.total = eqData.totalElecData;
    eqsDiscBatData = eqData.totalElecData;
    if((curTm->tm_year + 1900) == eqData.year){
        discElecQuttData.bat.year = eqData.yearElecData;
    }
    if((curTm->tm_mon + 1) == eqData.month){
        discElecQuttData.bat.month = eqData.monthElecData;
    }
    if(curTm->tm_mday == eqData.day){
        discElecQuttData.bat.day = eqData.dayElecData;
    }

    eqData.dayElecData = 0;
    eqData.year = yesterdayTm.tm_year + 1900;
    eqData.month = yesterdayTm.tm_mon + 1;
    eqData.day = yesterdayTm.tm_mday;
    readElecQuttData_db(&eqData, ELEC_QUTT_DATA_DAY);
    discElecQuttData.bat.yesterday = eqData.dayElecData;

    memset(&eqData, 0, sizeof(struct elecQuttDataDB));
    strcpy(eqData.chargeType, ELEC_QUTT_CHRG);
    strcpy(eqData.branchType, ELEC_QUTT_AC);
    readElecQuttData_db(&eqData, ELEC_QUTT_DATA_RESTORE);
    chrgElecQuttData.ac.total = eqData.totalElecData;
    eqsChrgAcData = eqData.totalElecData;
    if((curTm->tm_year + 1900) == eqData.year){
        chrgElecQuttData.ac.year = eqData.yearElecData;
    }
    if((curTm->tm_mon + 1) == eqData.month){
        chrgElecQuttData.ac.month = eqData.monthElecData;
    }
    if(curTm->tm_mday == eqData.day){
        chrgElecQuttData.ac.day = eqData.dayElecData;
    }

    eqData.dayElecData = 0;
    eqData.year = yesterdayTm.tm_year + 1900;
    eqData.month = yesterdayTm.tm_mon + 1;
    eqData.day = yesterdayTm.tm_mday;
    readElecQuttData_db(&eqData, ELEC_QUTT_DATA_DAY);
    chrgElecQuttData.ac.yesterday = eqData.dayElecData;

    memset(&eqData, 0, sizeof(struct elecQuttDataDB));
    strcpy(eqData.chargeType, ELEC_QUTT_CHRG);
    strcpy(eqData.branchType, ELEC_QUTT_BAT);
    readElecQuttData_db(&eqData, ELEC_QUTT_DATA_RESTORE);
    chrgElecQuttData.bat.total = eqData.totalElecData;
    eqsChrgBatData = eqData.totalElecData;
    if((curTm->tm_year + 1900) == eqData.year){
        chrgElecQuttData.bat.year = eqData.yearElecData;
    }
    if((curTm->tm_mon + 1) == eqData.month){
        chrgElecQuttData.bat.month = eqData.monthElecData;
    }
    if(curTm->tm_mday == eqData.day){
        chrgElecQuttData.bat.day = eqData.dayElecData;
    }

    eqData.dayElecData = 0;
    eqData.year = yesterdayTm.tm_year + 1900;
    eqData.month = yesterdayTm.tm_mon + 1;
    eqData.day = yesterdayTm.tm_mday;
    readElecQuttData_db(&eqData, ELEC_QUTT_DATA_DAY);
    chrgElecQuttData.bat.yesterday = eqData.dayElecData;

}

/**
 * brief - 详细电量数据获取
 * param - isChrgOrDisc: 充电或放电(IS_DISC-放电 IS_CHRG-充电)
 *         branchType: 支路类别(BRANCH_TYPE_AC-交流支路 BRANCH_TYPE_BAT-电池支路 BRANCH_TYPE_PV-光伏支路)
 *         queryDataType_t: 什么时间类别(ELEC_QUTT_DATA_HOUR-时 ELEC_QUTT_DATA_DAY-日 ELEC_QUTT_DATA_MONTH-月 ELEC_QUTT_DATA_YEAR-年)
 * return - 电量值
 */
float eqsQuttDataDetailGet(isChrgOrDisc_t isChrgOrDisc, branchType_t branchType, queryDataType_t queryDataType, struct elecQuttDataDB* peqdata){

    time_t curTime;
    struct tm curTm;

    curTime = time(NULL);
    localtime_r(&curTime, &curTm);

    if(isChrgOrDisc == IS_DISC){
        strcpy(peqdata->chargeType, ELEC_QUTT_DISC);
        if(branchType == BRANCH_TYPE_AC){
            strcpy(peqdata->branchType, ELEC_QUTT_AC);
            switch(queryDataType){
                case ELEC_QUTT_DATA_HOUR:
                    if((peqdata->hour == curTm.tm_hour) && (peqdata->day == curTm.tm_mday) && (peqdata->month == curTm.tm_mon + 1) && (peqdata->year == curTm.tm_year + 1900)){
                        return discElecQuttData.ac.hour;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->hourElecData;
                    }

                case ELEC_QUTT_DATA_DAY:
                    if((peqdata->day == curTm.tm_mday) && (peqdata->month == curTm.tm_mon + 1) && (peqdata->year == curTm.tm_year + 1900)){
                        return discElecQuttData.ac.day;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->dayElecData;
                    }

                case ELEC_QUTT_DATA_MONTH:
                    if((peqdata->month == curTm.tm_mon + 1) && (peqdata->year == curTm.tm_year + 1900)){
                        return discElecQuttData.ac.month;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->monthElecData;
                    }

                case ELEC_QUTT_DATA_YEAR:
                    if(peqdata->year == curTm.tm_year + 1900){
                        return discElecQuttData.ac.year;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->yearElecData;
                    }

                default:
                    return 0.0F;
            }
        }
        else if(branchType == BRANCH_TYPE_BAT){
            strcpy(peqdata->branchType, ELEC_QUTT_BAT);
            switch(queryDataType){
                case ELEC_QUTT_DATA_HOUR:
                    if((peqdata->hour == curTm.tm_hour) && (peqdata->day == curTm.tm_mday) && (peqdata->month == curTm.tm_mon + 1) && (peqdata->year == curTm.tm_year + 1900)){
                        return discElecQuttData.ac.hour;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->hourElecData;
                    }

                case ELEC_QUTT_DATA_DAY:
                    if((peqdata->day == curTm.tm_mday) && (peqdata->month == curTm.tm_mon + 1) && (peqdata->year == curTm.tm_year + 1900)){
                        return discElecQuttData.ac.day;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->dayElecData;
                    }

                case ELEC_QUTT_DATA_MONTH:
                    if((peqdata->month == curTm.tm_mon + 1) && (peqdata->year == curTm.tm_year + 1900)){
                        return discElecQuttData.ac.month;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->monthElecData;
                    }

                case ELEC_QUTT_DATA_YEAR:
                    if(peqdata->year == curTm.tm_year + 1900){
                        return discElecQuttData.ac.year;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->yearElecData;
                    }

                default:
                    return 0.0F;
            }
        }
        else{
            return 0.0F;
        }
    }
    else if(isChrgOrDisc == IS_CHRG){
        strcpy(peqdata->chargeType, ELEC_QUTT_CHRG);
        if(branchType == BRANCH_TYPE_AC){
            strcpy(peqdata->branchType, ELEC_QUTT_AC);
            switch(queryDataType){
                case ELEC_QUTT_DATA_HOUR:
                    if((peqdata->hour == curTm.tm_hour) && (peqdata->day == curTm.tm_mday) && (peqdata->month == curTm.tm_mon + 1) && (peqdata->year == curTm.tm_year + 1900)){
                        return discElecQuttData.ac.hour;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->hourElecData;
                    }

                case ELEC_QUTT_DATA_DAY:
                    if((peqdata->day == curTm.tm_mday) && (peqdata->month == curTm.tm_mon + 1) && (peqdata->year == curTm.tm_year + 1900)){
                        return discElecQuttData.ac.day;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->dayElecData;
                    }

                case ELEC_QUTT_DATA_MONTH:
                    if((peqdata->month == curTm.tm_mon + 1) && (peqdata->year == curTm.tm_year + 1900)){
                        return discElecQuttData.ac.month;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->monthElecData;
                    }

                case ELEC_QUTT_DATA_YEAR:
                    if(peqdata->year == curTm.tm_year + 1900){
                        return discElecQuttData.ac.year;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->yearElecData;
                    }

                default:
                    return 0.0F;
            }
        }
        else if(branchType == BRANCH_TYPE_BAT){
            strcpy(peqdata->branchType, ELEC_QUTT_BAT);
            switch(queryDataType){
                case ELEC_QUTT_DATA_HOUR:
                    if((peqdata->hour == curTm.tm_hour) && (peqdata->day == curTm.tm_mday) && (peqdata->month == curTm.tm_mon + 1) && (peqdata->year == curTm.tm_year + 1900)){
                        return discElecQuttData.ac.hour;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->hourElecData;
                    }

                case ELEC_QUTT_DATA_DAY:
                    if((peqdata->day == curTm.tm_mday) && (peqdata->month == curTm.tm_mon + 1) && (peqdata->year == curTm.tm_year + 1900)){
                        return discElecQuttData.ac.day;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->dayElecData;
                    }

                case ELEC_QUTT_DATA_MONTH:
                    if((peqdata->month == curTm.tm_mon + 1) && (peqdata->year == curTm.tm_year + 1900)){
                        return discElecQuttData.ac.month;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->monthElecData;
                    }

                case ELEC_QUTT_DATA_YEAR:
                    if(peqdata->year == curTm.tm_year + 1900){
                        return discElecQuttData.ac.year;
                    }
                    else{
                        readElecQuttData_db(peqdata, queryDataType);
                        return peqdata->yearElecData;
                    }

                default:
                    return 0.0F;
            }
        }
        else{
            return 0.0F;
        }
    }
    else{
        return 0.0F;
    }
}

//今日电量获取
float eqsQuttDataTodayGet(isChrgOrDisc_t isChrgOrDisc, branchType_t branchType){

    if(isChrgOrDisc == IS_DISC){
        if(branchType == BRANCH_TYPE_AC){
            return discElecQuttData.ac.day;
        }
        else if(branchType == BRANCH_TYPE_BAT){
            return discElecQuttData.bat.day;
        }
        else{
            return 0.0F;
        }
    }
    else if(isChrgOrDisc == IS_CHRG){
        if(branchType == BRANCH_TYPE_AC){
            return chrgElecQuttData.ac.day;
        }
        else if(branchType == BRANCH_TYPE_BAT){
            return chrgElecQuttData.bat.day;
        }
        else{
            return 0.0F;
        }
    }
    else{
        return 0.0F;
    }
}

float eqsQuttDataYesterdayGet(isChrgOrDisc_t isChrgOrDisc, branchType_t branchType){

    if(isChrgOrDisc == IS_DISC){
        if(branchType == BRANCH_TYPE_AC){
            return discElecQuttData.ac.yesterday;
        }
        else if(branchType == BRANCH_TYPE_BAT){
            return discElecQuttData.bat.yesterday;
        }
        else{
            return 0.0F;
        }
    }
    else if(isChrgOrDisc == IS_CHRG){
        if(branchType == BRANCH_TYPE_AC){
            return chrgElecQuttData.ac.yesterday;
        }
        else if(branchType == BRANCH_TYPE_BAT){
            return chrgElecQuttData.bat.yesterday;
        }
        else{
            return 0.0F;
        }
    }
    else{
        return 0.0F;
    }
}

//当月电量获取
float eqsQuttDataMonthGet(isChrgOrDisc_t isChrgOrDisc, branchType_t branchType){

    if(isChrgOrDisc == IS_DISC){
        if(branchType == BRANCH_TYPE_AC){
            return discElecQuttData.ac.month;
        }
        else if(branchType == BRANCH_TYPE_BAT){
            return discElecQuttData.bat.month;
        }
        else{
            return 0.0F;
        }
    }
    else if(isChrgOrDisc == IS_CHRG){
        if(branchType == BRANCH_TYPE_AC){
            return chrgElecQuttData.ac.month;
        }
        else if(branchType == BRANCH_TYPE_BAT){
            return chrgElecQuttData.bat.month;
        }
        else{
            return 0.0F;
        }
    }
    else{
        return 0.0F;
    }
}

//当年电量获取
float eqsQuttDataYearGet(isChrgOrDisc_t isChrgOrDisc, branchType_t branchType){

    if(isChrgOrDisc == IS_DISC){
        if(branchType == BRANCH_TYPE_AC){
            return discElecQuttData.ac.year;
        }
        else if(branchType == BRANCH_TYPE_BAT){
            return discElecQuttData.bat.year;
        }
        else{
            return 0.0F;
        }
    }
    else if(isChrgOrDisc == IS_CHRG){
        if(branchType == BRANCH_TYPE_AC){
            return chrgElecQuttData.ac.year;
        }
        else if(branchType == BRANCH_TYPE_BAT){
            return chrgElecQuttData.bat.year;
        }
        else{
            return 0.0F;
        }
    }
    else{
        return 0.0F;
    }
}

//总电量获取
float eqsQuttDataTotalGet(isChrgOrDisc_t isChrgOrDisc, branchType_t branchType){

    if(isChrgOrDisc == IS_DISC){
        if(branchType == BRANCH_TYPE_AC){
            return discElecQuttData.ac.total;
        }
        else if(branchType == BRANCH_TYPE_BAT){
            return discElecQuttData.bat.total;
        }
        else{
            return 0.0F;
        }
    }
    else if(isChrgOrDisc == IS_CHRG){
        if(branchType == BRANCH_TYPE_AC){
            return chrgElecQuttData.ac.total;
        }
        else if(branchType == BRANCH_TYPE_BAT){
            return chrgElecQuttData.bat.total;
        }
        else{
            return 0.0F;
        }
    }
    else{
        return 0.0F;
    }
}

struct elecQuttDataDB* eqsQuttDataDBGet(void)
{
    return &eqsQuttDataDB;
}

void eqsQuttDataDBInit(void)
{
    memset(&eqsQuttDataDB, 0, sizeof(struct elecQuttDataDB));
}

static timer_t elecTimerId = NULL;

void electricityQuantityStatisticsThread(void* pPara)
{
    Misc_InitThread(__func__);
       
    time_t curTime;
    struct tm curTm;
    struct sigevent sevp;
    struct itimerspec it;

    signal(SIGALRM, statisticsTimerThread);

    if(timerInit(&elecTimerId, &sevp, &it) == -1){
        printf("timerInit failed!\n");
        return;
    }

    if(timerStart(&elecTimerId, &it) == -1){
        printf("timerStart failed!\n");
        return;
    }

    memset(&eqsTm, 0, sizeof(struct tm));
    curTime = time(NULL);
    localtime_r(&curTime, &eqsTm);

    elecQuttDataRestore(&eqsTm);

    eqsQuttDataDBInit();
    
    printf("electricity quantity statistics thread is running...[%d-%d-%d %d:%d:%d]\n", 
        eqsTm.tm_year + 1900, eqsTm.tm_mon + 1, eqsTm.tm_mday, eqsTm.tm_hour, eqsTm.tm_min, eqsTm.tm_sec);

    while(true){

        curTime = time(NULL);
        localtime_r(&curTime, &curTm);

        if(isHourChanged(&curTm) == FLAG_CHANGE){
            elecQuttDataSave();
            printf("存储电量%s", asctime(&curTm));
            discElecQuttData.ac.hour = 0;
            chrgElecQuttData.ac.hour = 0;
            discElecQuttData.bat.hour = 0;
            chrgElecQuttData.bat.hour = 0;
        }

        if(isDayChanged(&curTm) == FLAG_CHANGE){
            discElecQuttData.ac.yesterday = discElecQuttData.ac.day;
            chrgElecQuttData.ac.yesterday = chrgElecQuttData.ac.day;
            discElecQuttData.bat.yesterday = discElecQuttData.bat.day;
            chrgElecQuttData.bat.yesterday = chrgElecQuttData.bat.day;
            discElecQuttData.ac.day = 0;
            chrgElecQuttData.ac.day = 0;
            discElecQuttData.bat.day = 0;
            chrgElecQuttData.bat.day = 0;
        }

        if(isMonthChanged(&curTm) == FLAG_CHANGE){
            discElecQuttData.ac.month = 0;
            chrgElecQuttData.ac.month = 0;
            discElecQuttData.bat.month = 0;
            chrgElecQuttData.bat.month = 0;
        }

        if(isYearChanged(&curTm) == FLAG_CHANGE){
            discElecQuttData.ac.year = 0;
            chrgElecQuttData.ac.year = 0;
            discElecQuttData.bat.year = 0;
            chrgElecQuttData.bat.year = 0;
        }
        
       usleep(500000);
    }
}